﻿using LS.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebMatrix.WebData;

namespace LS.ModelLogicHelpers {
    public class MyCardLogicHelper {
        private LSDbContext db = new LSDbContext();

        object _lock = new object();
        public IEnumerable<CardManageModel> GetCardsByUser() {

            var rtn = new List<CardManageModel>();
            var _currentMember = db.Members.Where(item => item.UsrId == WebSecurity.CurrentUserId).FirstOrDefault();
            if (_currentMember == null)
                return rtn;
            else {
                var _memberCards = from mcs in db.MemberCards
                                   where mcs.MemberId == _currentMember.Id
                                   select new CardManageModel() {
                                       MemberCardId = mcs.Id,
                                       Card = db.Cards.Where(item => item.Id == mcs.CardId).FirstOrDefault(),
                                       Description = mcs.Description,
                                       ExpireDate = mcs.ExpireDate,
                                       Status = db.LostFoundHistories.Where(item => item.MemberCardId == mcs.Id && item.Status == 0).FirstOrDefault() == null ?
                                       Status.InService : mcs.ExpireDate < DateTime.Now ? Status.NeedRenew : Status.Found
                                   };
                return _memberCards.OrderByDescending(item=>item.Card.UpdateDate).ToList<CardManageModel>();

            }
        }
        public bool AddNewCardForMember(CardManageModel newCard) {
            bool rtn = false;
            try {
                newCard.Card.UpdateDate = DateTime.Now;
                db.Cards.Add(newCard.Card);
                var _currentMember = db.Members.Where(item => item.UsrId == WebSecurity.CurrentUserId).FirstOrDefault();
                if (_currentMember == null) {
                    _currentMember = new Member() {
                        UsrId = WebSecurity.CurrentUserId
                    };
                    db.Members.Add(_currentMember);
                }
                var newMemberCard = new MemberCard();

                newMemberCard.ExpireDate = newCard.ExpireDate;

                newMemberCard.Description = newCard.Description;
                newMemberCard.RelatedCard = newCard.Card;
                newMemberCard.RelatedMember = _currentMember;

                db.MemberCards.Add(newMemberCard);

                db.SaveChanges();
                rtn = true;
            } catch {
                rtn = false;
            }

            return rtn;
        }

        public CardManageModel GetDetail(int MemberCardId) {
            var rtn = new CardManageModel();
            var membercard = db.MemberCards.Find(MemberCardId);
            membercard.RelatedCard = db.Cards.Find(membercard.CardId);
            rtn.MemberCardId = MemberCardId;
            rtn.Card = membercard.RelatedCard;
            rtn.Description = membercard.Description;
            rtn.ExpireDate = membercard.ExpireDate;
            return rtn;
        }
        public bool Update(CardManageModel updatedEntity)
        {
            var membercard = db.MemberCards.Find(updatedEntity.MemberCardId);
            membercard.Description = updatedEntity.Description;
            var rtn = db.SaveChanges() > 0;
            return rtn;
        }

        public MemberCard GetMemberCardBySerialNumber(string SerialNumber) {
            var card = db.Cards.Where(item => item.SeiralNumber == SerialNumber).FirstOrDefault();
            var rtn = db.MemberCards.Where(item => item.CardId == card.Id).First();
            return rtn;
        }

        public bool RenewCard(int MemberCardId = 0) {
            var membercard = db.MemberCards.Find(MemberCardId);
            membercard.ExpireDate = membercard.ExpireDate.AddYears(1);
            return db.SaveChanges() > 0;
        }
    }
}